package top.aicats.pattern.dynamicProxy.JDKMeiPo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class JDKMeipo implements InvocationHandler {
    private Object target;

    public Object getInstance(Object target) throws  Exception {
        this.target = target;
        Class<?> clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.target, args);
        after();
        return obj;
    }

    private void after() {
        System.out.println("合适的话就开始准备婚事啦 after\r\n");
    }

    private void before() {
        System.out.println("媒婆开始物色对象啦： before\r\n");
    }
}
